package edu.xkd.slice;

import edu.xkd.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.global.resource.NotExistException;
import ohos.global.resource.Resource;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;

public class MainAbilitySlice extends AbilitySlice {
    Text text_show = null;
    Button button_01 = null;
    String[] jocks = {};
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        StringBuilder sb = new StringBuilder();
        BufferedReader br = null;
        Resource resource = null;

        try {
            resource = this.getResourceManager().getResource(ResourceTable.Profile_base);
            br = new BufferedReader(new InputStreamReader(resource));

            String content = "";
            while((content = br.readLine()) != null){
                sb.append(content);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                br.close();
                resource.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        //切分
        jocks = sb.toString().split("---");

        //获取layout中的对象
        text_show = (Text)this.findComponentById(ResourceTable.Id_text_show);
        button_01 = (Button)this.findComponentById(ResourceTable.Id_button_01);
        //添加点击事件
        button_01.setClickedListener(this::onClickListenerButton01);


    }

    private void onClickListenerButton01(Component component) {
        //随机产生一个下标
        int len = jocks.length;
        Random random = new Random();
        String jock = jocks[random.nextInt(len)];

        text_show.setText(jock);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
